/*
* @Author: obsidian
* @Date:   2016-03-16 17:48:00
* @Last Modified by:   obsidian
* @Last Modified time: 2016-04-07 22:16:44
*/
#include "store.h"
#include "module/pid/pid.h"
#include "module/GPS/GPS.h"
#include "module/brake/brake.h"
#include "module/steer/steer.h"
#include "control/GPSpath/GPSpath.h"
#include "module/pid/pid.h"
#include "module/camera/camera.h"
#include "module/lcd/numstr.h"
static unsigned char filename[13]   = "path0.txt";
static unsigned char file[25]     = "";
static unsigned char try[25]      = "";
static unsigned char str_path[25] = "";
static void str_init(void);
static void str_save(void);
static void str_close(void);
static void str_getpath(void);


struct Store store = 
{
	.str_fl   = {0},          //fil
	.try_cnt  = 0,
	.init     = str_init,   //init
	.save     = str_save,   //save
	.close    = str_close,  //close
	.get_path = str_getpath
};

static int get_mday(char date[])
{
	unsigned char k = 0;
	if(date[4] == 0x20)
	{
		k = 0;
	}
	else{ k = date[4]; }
	return k*10 + (date[5]-'0');
}
static int get_year(char date[])
	{return (date[7]-'0')*1000 + (date[8]-'0')*100 + (date[9]-'0')*10 + (date[10]-'0');}
static int get_hour(char time[])
	{return (time[0]-'0')*10 + (time[1]-'0');}
static int get_min(char time[])
	{return (time[3]-'0')*10 + (time[4]-'0');}
static int get_sec(char time[])
	{return (time[6]-'0')*10 + (time[7]-'0');}

void str_joint(unsigned char dst[],unsigned char src[],unsigned char len)
{
	unsigned char i = 0;
	for(i = 0;i < len && src[i] != 0;i ++)
	{
		dst[i] = src[i];
	}
}

short date = 0;
unsigned short path_index = 0;

static void str_getpath(void)
{
	str_joint(&str_path[path_index], __DATE__, 3);
	path_index += 3;
	num.intvalue = get_mday(__DATE__);
	num.int2str();
	str_joint(&str_path[path_index], num.numstr, num.index);
	path_index += num.index;
	str_path[path_index] = '_';
	path_index ++;  
	num.intvalue = get_hour(__TIME__);
	num.int2str();
	str_joint(&str_path[path_index], num.numstr, num.index);
	path_index += num.index;
	str_path[path_index] = '_';
	path_index ++;  
	num.intvalue = get_min(__TIME__);
	num.int2str();
	str_joint(&str_path[path_index], num.numstr, num.index);
	path_index += num.index;
	str_joint(file, str_path, path_index);
	file[path_index] = '/';
	str_joint(try, str_path, path_index);
	try[path_index] = '/';
	path_index ++;

	str_joint(&try[path_index], "try", 3);
	f_mkdir(str_path);
}

void str_init()
{
	FIL             try_fil;
	unsigned int  k     = 0;
	f_open(&try_fil,try,FA_READ);
	if(try_fil.fs != 0){f_read(&try_fil,&store.try_cnt, sizeof(store.try_cnt), &k);}
	f_close(&try_fil);

	filename[4] = store.try_cnt + '0';
	str_joint(&file[path_index],filename,13);
	f_open(&store.str_fl, file, FA_CREATE_ALWAYS|FA_WRITE);
	store.try_cnt ++;
	if(store.try_cnt > 9){store.try_cnt = 0;}
}

void str_save()
{
	if(store.str_fl.fs == 0){return;}
	f_printf(&store.str_fl, "[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d],\n", 
							(int)(gps.front_point.x * 10),
							(int)(gps.front_point.y * 10), 
							(int)(gpspath.target_point.x * 10),
							(int)(gpspath.target_point.y * 10), 
							(int)(gps.angle * 10),
							(int)(gps.speed * 10),
							(int)(gpspath.distance_error * 10),
							(int)(gpspath.angle_error * 10),
							(int)(steer.angle * 10),
							(int)(camera.offset * 10),
							(int)(gpspath.target_angle * 10)
							);
			//f_sync(&store.str_fl);
}

void str_close()
{

	FIL             try_fil;
	unsigned int k     = 0;
	f_open(&try_fil,try,FA_CREATE_ALWAYS|FA_WRITE);
	if(try_fil.fs != 0){f_write(&try_fil,&store.try_cnt, sizeof(store.try_cnt), &k);}
	f_close(&try_fil);

	if(store.str_fl.fs == 0){return;}
	f_close(&store.str_fl);
}
